#ifndef _MYLOG_H_
#define _MYLOG_H_

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
#include <semaphore.h>

#if defined __cplusplus
extern "C" {
#endif

// extern int zucLogMark(const char *fmt, ...);    // log start & log end
#define ZLOGLV_E 0
#define ZLOGLV_W 1
#define ZLOGLV_I 2
#define ZLOGLV_D 3
#define ZLOGLV_T 4
#define ZLOGLV_N 5

extern int zucLogError(unsigned int code, const char* fmt, ...);
extern int zucLogWarning(unsigned int code, const char* fmt, ...);
extern int zucLogInfo(unsigned int code, const char* fmt, ...);
extern int __zucInnerDbg(unsigned char lv, unsigned int line, const char* filename, const char* fmt, ...);
#define zucInnerDbg(fmt, ...) __zucInnerDbg(ZLOGLV_D, __LINE__, __FILE__, fmt, ##__VA_ARGS__)
#define zucInnerErr(fmt, ...) __zucInnerDbg(ZLOGLV_E, __LINE__, __FILE__, fmt, ##__VA_ARGS__)
#define zucInnerInfo(fmt, ...) __zucInnerDbg(ZLOGLV_I, __LINE__, __FILE__, fmt, ##__VA_ARGS__)

#define zuclog_error(tag, fmt, ...) __zucInnerDbg(ZLOGLV_E, 0, "", "[" tag "] " fmt, ##__VA_ARGS__)
#define zuclog_warn(tag, fmt, ...) __zucInnerDbg(ZLOGLV_W, 0, "", "[" tag "] " fmt, ##__VA_ARGS__)
#define zuclog_info(tag, fmt, ...) __zucInnerDbg(ZLOGLV_I, 0, "", "[" tag "] " fmt, ##__VA_ARGS__)
#define zuclog_debug(tag, fmt, ...) __zucInnerDbg(ZLOGLV_D, 0, "", "[" tag "] " fmt, ##__VA_ARGS__)
#define zuclog_trace(tag, fmt, ...) __zucInnerDbg(ZLOGLV_T, 0, "", "[" tag "] " fmt, ##__VA_ARGS__)

#define zlog_error(fmt, ...) __zucInnerDbg(ZLOGLV_E, 0, "", fmt, ##__VA_ARGS__)
#define zlog_warn(fmt, ...) __zucInnerDbg(ZLOGLV_W, 0, "", fmt, ##__VA_ARGS__)
#define zlog_info(fmt, ...) __zucInnerDbg(ZLOGLV_I, 0, "", fmt, ##__VA_ARGS__)
#define zlog_debug(fmt, ...) __zucInnerDbg(ZLOGLV_D, 0, "", fmt, ##__VA_ARGS__)
#define zlog_trace(fmt, ...) __zucInnerDbg(ZLOGLV_T, 0, "", fmt, ##__VA_ARGS__)

// 用例：zlog_limit(zlog_trace("hello world %d",1),10) 仅前10次调用会输出日志
#define zlog_limit(_FF, _CNT__)            \
    {                                      \
        static int __CNTFLG_____ = _CNT__; \
        if (__CNTFLG_____ > 0)             \
        {                                  \
            _FF;                           \
            __CNTFLG_____--;               \
        }                                  \
    }

int zucLogInit();
int zucSetLogLevel(unsigned char logLevel);
int zucEnableStdout(unsigned char en);
#if defined __cplusplus
}
#endif

#endif
